﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Economo.Services.DataContext;
using Economo.Services.EntitiesConversion;

namespace Economo.Services.BusinessLogic
{
    public class AccountTypesBusinessLogic
    {       
        public BusinessEntities.AccountType GetAccountTypeById(byte id)
        {
            AccountTypesDataContext da = new AccountTypesDataContext();
            try
            {
                DataContext.AccountType accountType = da.GetAccountTypeById(id);
                BusinessEntities.AccountType result = Convertions.ConvertToBusinessEntity<BusinessEntities.AccountType>(accountType);
                return result;
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageBusinessLogicException(ex);
                return null;
            }
            finally
            {
                da.Dispose();
            }

        }

        public byte InsertAccountType(BusinessEntities.AccountType newAccountType)
        {
            AccountTypesDataContext da = new AccountTypesDataContext();
            try
            {
                AccountType AccountType = Convertions.ConvertToDataEntity<AccountType>(newAccountType);
                newAccountType.Id = da.InsertAccountType(AccountType);
                return newAccountType.Id;
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageBusinessLogicException(ex);
                return 0;
            }
            finally
            {
                da.Dispose();
            }
        }

        public void UpdateAccountType(BusinessEntities.AccountType updatedAccountType)
        {
            AccountTypesDataContext da = new AccountTypesDataContext();
            try
            {
                AccountType AccountType = Convertions.ConvertToDataEntity<AccountType>(updatedAccountType);
                da.UpdateAccountType(AccountType);
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageBusinessLogicException(ex);
            }
            finally
            {
                da.Dispose();
            }
        }

        public void DeleteAccountType(byte id)
        {
            AccountTypesDataContext da = new AccountTypesDataContext();
            try
            {
                da.DeleteAccountType(id);
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageBusinessLogicException(ex);
            }
            finally
            {
                da.Dispose();
            }

        }
    }
}
